//
//  CLNavigationBar.swift
//  clreader
//
//  Created by colin on 2019/2/26.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

class CLNavigationBar: UIView {

    class Item : NSObject {
        
        var view : UIView
        var handle : (()->Void)?
        
        init(customView:UIView,viewHandle:(()->Void)?) {
            self.view = customView
            self.handle = viewHandle
        }
        
        private override init() {
            self.view = UIView()
            super.init()
        }
        
    }
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.initialization()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initialization()
    }
    
    //容器
    private lazy var barContentView = UIView()
    
    //标题
    private lazy var titleLabel = UILabel()
    
    // 返回按钮
    private lazy var buttonBack = UIButton(type: .custom)
    
    var title = "" {
        didSet {
            self.titleLabel.text = self.title
        }
    }
    
    //MARK: - 返回按钮点击
    @objc var backHandle:(()->Void)? {
        didSet {
            self.buttonBack.isHidden = false
        }
    }
    
    //MARK: - 右边按钮
    @objc var rightItems = [Item]() {
        didSet {
            //移除之前的
            for view_ in self.rightItemViews {
                view_.removeFromSuperview()
            }
            self.rightItemViews.removeAll()
            
            //添加
            var preView:UIView?
            for index in 0 ..< self.rightItems.count {
                let item = self.rightItems[index]
                self.barContentView.addSubview(item.view)
                item.view.snp.makeConstraints({ (make) in
                    make.top.bottom.equalToSuperview()
                    make.width.greaterThanOrEqualTo(44.0)
                    
                    if let preView_ = preView {
                        make.left.equalTo(preView_.snp.right)
                    }
                    
                    if index == self.rightItems.count - 1 {
                        make.right.equalToSuperview().offset(-10.0)
                    }
                })
                
                preView = item.view
                self.rightItemViews.append(item.view)
                item.view.tag = index
                
                if let buttonItem = item.view as? UIButton {
                    buttonItem.addTarget(self, action: #selector(self.rightItemClick(sender:)), for: .touchUpInside)
                } else {
                    let ges = UITapGestureRecognizer(target: self, action: #selector(self.rightItemClick(sender:)))
                    item.view.addGestureRecognizer(ges)
                }
            }
        }
    }
    private var rightItemViews = [UIView]()
    
    @objc private func rightItemClick(sender:Any) {
        var index:Int?
        if let buttonItem = sender as? UIButton {
            index = buttonItem.tag
        }
        if let ges = sender as? UIGestureRecognizer {
            index = ges.view?.tag
        }
        if let index_ = index {
            self.rightItems[index_].handle?()
        }
    }
    
    //MARK: - 初始化
    private func initialization() {
    
        self.backgroundColor = UIColor(hex: "ffffff")
        
        self.barContentView.backgroundColor = UIColor.clear
        
        self.addSubview(barContentView)
        self.barContentView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(44.0)
        }
        
        //标题
        self.titleLabel.text = self.title
        self.titleLabel.font = UIFont.systemFont(ofSize: 17.0, weight:.bold)
        self.titleLabel.textColor = UIColor(hex:"343434")
        self.barContentView.addSubview(self.titleLabel)
        self.titleLabel.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
        }
        
        //返回按钮
        self.buttonBack.setImage(UIImage(named:"button_navigation_back"), for: .normal)
        self.buttonBack.isHidden = true
        self.barContentView.addSubview(self.buttonBack)
        self.buttonBack.addTarget(self, action: #selector(self.buttonBackClick(sender:)), for: .touchUpInside)
        self.buttonBack.snp.makeConstraints { (make) in
            make.bottom.top.equalToSuperview()
            make.left.equalToSuperview().offset(10.0)
            make.width.equalTo(44.0)
        }
        
        //底部分割线
        let bottomLine = UIView()
        bottomLine.backgroundColor = UIColor(hex: "e6e6e6")
        self.addSubview(bottomLine)
        bottomLine.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    @objc private func buttonBackClick(sender:Any) {
        self.backHandle?()
    }
    
    //MARK: - 显示
    @objc func show(inView:UIView?) {
        guard let superview_ = inView else {
            return
        }
        superview_.addSubview(self)
        self.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            if #available(iOS 11.0, *) {
                make.bottom.equalTo(superview_.safeAreaLayoutGuide.snp.top).offset(44.0)
            } else {
                make.height.equalTo(64.0)
            }
        }
    }
    
}
